package com.mind.common.utils.sign;

import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONException;
import lombok.extern.log4j.Log4j2;

@Log4j2
public class SignatureUtils {

    public static String validSignedStr(String signedStr) throws Exception {
        AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, "qnxhm3tuq43sqtr3".getBytes(), "4o3tk3wohx4ed9u4".getBytes());
        String signeStr = aes.decryptStr(signedStr);
        try {
            log.info(signedStr);
            Signed signed = JSON.parseObject(signeStr, Signed.class);
            if (signed == null){
                return "签名格式错误";
            }
//            if(signed.getUserName().equals("zhangsan")){
//                return "";
//            }
//            Long nowTimestamp = new Date().getTime();
//            if(signed.getTimestamp() == null){
//                return "签名格式不正确";
//            }
//            Long timestamp = signed.getTimestamp() * 1000L;
//            if (timestamp + 10 * 1000L < nowTimestamp) {
//                return "签名已过期";
//            }
        } catch (JSONException e) {
            log.error("签名数据有误：{}", signedStr);
            return "签名数据有误";
        }
        return "";
    }
}
